package com.newzqxq.koalamoney.widget.toast;

import android.content.Context;
import android.support.annotation.IntDef;

import com.newzqxq.koalamoney.R;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Toast 提示协助类
 * <p>
 * 方法：
 * <p>
 * {@link #toast(int, CharSequence) 设置提示类型以及文字，然后显示 }
 * {@link #cancel() 取消 }
 * <p>
 * Created by 叶开 on 2017/6/7.
 */

public final class Speaker {

    private HintToast toast = null;

    public static final int SUCCESS = 0;//成功类型
    public static final int ERROR = 1;//错误类型
    public static final int FAILED = 2;//失败类型
    public static final int NORMAL = 3;//普通类型

    @IntDef({SUCCESS, ERROR, FAILED, NORMAL})
    @Retention(RetentionPolicy.SOURCE)
    @Target(ElementType.PARAMETER)
    public @interface Type {
        //提示类型
    }

    private Context context;

    /**
     * 构造函数
     */
    public Speaker(Context context) {
        this.context = context;
    }

    /**
     * 设置提示类型以及文字
     *
     * @param type 提示类型
     * @param text 提示文字
     */
    public void toast(@Type int type, CharSequence text) {
        cancel();
        toast = new HintToast(context);
        toast.setMessage(text);
        switch (type) {
            case SUCCESS:
                //成功
                toast.setLogo(R.drawable.img_tz_succss);
                toast.setMessage(text);
                break;
            case ERROR:
                //警告
                toast.setLogo(R.drawable.img_tz_gantan);
                toast.setMessage(text);
                break;
            case FAILED:
                //失败
                break;
            case NORMAL:
                //普通
                toast.setLogo(R.drawable.img_tz_gantan);
                toast.setMessage(text);
                break;
        }
        toast.show();
    }

    /**
     * 取消
     */
    public void cancel() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }

}
